<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Commodity extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->seg_exp = segment_explode($this->uri->uri_string());
// 		$this->load->model('commodity/commodity_model');
	}


	public function _remap($method, $params = array())
	{
// 		$this->load->view('/common/header');
		//echo $method;
		if( method_exists($this, $method) ){

			return call_user_func_array(array($this, $method), $params);

		} else if ($method == 'page') {//※ページ

			//if( in_array('list', $this->seg_exp) ) {
			if( in_array('q', $this->seg_exp) )	{

				$this->list();

			} else  {

				$this->get_commodity($params);
			}
		} else {

			$this->get_commodity($method);
		}


// 		$this->load->view('/common/footer');
	}



	public function index() {
//echo "index";
		$this->get_commodity(array());

	}
	public function get_commodity($params = null) {

		if(is_array($params)){
			//ページ処理 start
			$paging_data['tbl_nm'] = 'commodity_detail';
			$paging_data['where'] = '';
			$paging_data['select'] = '*';
			$paging_data['orderItem'] = 'commodity_code';
			$paging_data['orderType'] = 'ASC';
			$paging_data['search_url']='';


		//カテゴリクリックの場合
		}else {

			//$paging_data['search_word'] = $search_word = urldecode($this->security->xss_clean(url_explode($this->seg_exp, 'q')));
			//주소에서 q 제거. 뷰에서 사용
			//$this->seg_exp =  url_delete($this->seg_exp, 'q');
			//$paging_data['search_url'] = "/q/".$search_word;

			//category
			$sql = "SELECT commodity_detail.* FROM commodity_detail
			JOIN commodity_header ON commodity_header.represent_sku_code = commodity_detail.sku_code
			JOIN category_commodity ON commodity_header.commodity_code = category_commodity.commodity_code
			AND category_commodity.category_code = ? ";
			//ORDER BY commodity_detail.sku_code ASC LIMIT 10";

			$paging_data['search_word'] = $search_word =$params;
			$paging_data['search_url'] = "";
			$paging_data['sql'] =$sql;
			$paging_data['param'] = $search_word;
			$paging_data['orderItem'] = 'commodity_detail.sku_code';
			$paging_data['orderType'] = 'ASC';
		}

		$data = make_pagination($this->seg_exp, $paging_data);
		//ページ処理 end

		$this->load->model('/admin/category_mo', 'category_mo');
		$data['category_tree'] = $this->category_mo->get_all_category('0', 1);


		$this->load->view('commodity/commodity', $data);

	}

	//検索orカテゴリクリック
	public function  lists($params=null) { //旧：($comodity_code = ''){

		$where = array();
		//ヘッダの検索からの場合
	

			$paging_data['search_word'] = $search_word = urldecode($this->security->xss_clean(url_explode($this->seg_exp, 'q')));
			//주소에서 q 제거. 뷰에서 사용
			$this->seg_exp =  url_delete($this->seg_exp, 'q');
			$paging_data['search_url'] = "/q/".$search_word;

			$where['or_like'] = array('commodity_code' => $search_word,
										'sku_code' => $search_word,
										'standard_detail1_code' => $search_word,
										'standard_detail2_code' => $search_word);

			$paging_data['tbl_nm'] = 'commodity_detail';
			$paging_data['where'] = $where;
			$paging_data['select'] = '*';
			$paging_data['orderItem'] = 'commodity_code';
			$paging_data['orderType'] = 'ASC';
	
	

		//ページ処理 start
		$data = make_pagination($this->seg_exp, $paging_data);
		//ページ処理 end

		//category
		$this->load->model('/admin/category_mo', 'category_mo');
		$data['category_tree'] = $this->category_mo->get_all_category('0', 1);

		$this->load->view('commodity/commodity_list', $data);//array('rs'=>$data));

	}

}

